<?php

if (!defined('BASEPATH'))
    exit('No direct script access allowed');

/**
 * Controller for managing roles.
 * @package cms-roles/conrollers
 * @category Controller
 * 
 * @author Mark Angelo Angulo
 * @copyright (c) 2014
 */
class Role extends CMS_Controller {

    /**
     *  Constructor method.
     */
    function __construct()
    {
        parent::__construct();
        
        $this->load->model('role_model');
        $this->load->model('cms-permissions/permission_model', 'permission_model');
        $this->load->model('cms-modules/module_model', 'module_model');
        
        $this->load->helper('cms');
        $this->load->helper('role');
        
        $this->load->library('form_validation');
//        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small class="help-block">', '</small></div>');
        $this->form_validation->set_error_delimiters('<div class="clear-fix"></div><small>', '</small>');
        
        init_role();
    }

    /**
     * Default method called when nothing is specified.
     * Displays dashboard.
     * @access public
     */
    public function index()
    {
//        $module = array();
//        $permissions = array();
//        
//        $permissions['view'] = 1;
//        $permissions['print'] = 1;
//        $permissions['csv'] = 0;
//        
//        $module['permissions'] = serialize($permissions);
//        
//        $this->module_model->update(array('id' => 1), $module);
//        echo 'sf'; exit;
        
        view_roles_list();
    }
    
    /*
     * Method called for adding a new record
     * @access public
     */
    public function add()
    {
        if( isset($_POST) && !empty($_POST) )
        {
            if ( valid_add_role() )
            {
                $role = array();
                set_value('name')        ? $role['name']          =   clean_text( set_value('name') )           : '';
                set_value('description') ? $role['description']   =   clean_text( set_value('description') )    : '';

//                $group_id = $this->ion_auth->create_group($name, $description);
                $group_id = $this->role_model->insert($role);
                
//                if( ! $group_id )
//                {
//                    $error = $this->ion_auth->errors();
//                    view_role_form('Add Group', 'Add Group', strip_tags($error));
//                }
//                else
//                {
                    $permissions    = $this->input->post('permissions');
                    $modules        = $this->module_model->get_all();
                    
                    foreach($modules as $module)
                    {
                        $role_permission                   = array();
                        $role_permission['group_id']       = $group_id;
                        $role_permission['module_id']      = $module['id'];
                        
                        $role_permissions                   = array();
                        $module_permissions                 = unserialize($module['permissions']);
                        
                        foreach($module_permissions as $module_permission)
                        {
                            if( isset( $permissions[$module['id']][$module_permission] ) )
                            {
                                $role_permissions[$module_permission] = $permissions[ $module['id'] ][$module_permission] === 'on' ? 1 : 0;
                            }
//                            else
//                            {
//                                $role_permissions[$module_permission] = NULL;
//                            }
                        }
                        $role_permission['group_permissions'] = serialize($role_permissions);
                        $this->permission_model->insert($role_permission);
                    }
                    
                    if($this->input->post('add_another'))
                    {
                        $this->form_validation->unset_field_data();
                        view_role_form('Add Group', 'Add Group', 'add_success');
                    } else
                    {
                        view_roles_list(NULL, 'add_success');
                    }
//                }
                
                // add another
                if( $this->input->post('add-another') )
                {
                    $this->form_validation->unset_field_data();
                    view_role_form('Add Role', 'add_success');
                } else
                {
                    view_roles_list(NULL, 'add_success');
                }
                
            } else
            {
                view_role_form('Add Role', 'add_failed');
            }
            
        } else
        {
            view_role_form('Add Role');
        }
    }
    
    /**
     * Method called for editing a new record.
     * @access public
     */
    public function edit()
    {
        $params = $this->uri->uri_to_assoc();

        if( isset($_POST) && ! empty($_POST) )
        {
            if ( valid_role('edit') )
            {
                $role = array();
                $role['name']                = clean_text( set_value('name') );
                $role['description']         = clean_text( set_value('description') );
//                $role['status']              = set_value('status');
                $role['date_modified']       = date('Y-n-d H:i:s');

                $this->role_model->update(array('id' =>set_value('id')), $role);
                view_roles_list(NULL, 'add_success');
            }
        } else
        {
            if( isset($params['id']) && valid_get_id($params['id']) )
            {
                $role = $this->role_model->get_by_id($params['id']);
                view_role_form('Edit Role', NULL, $role);
            } else
            {
                $this->show_404();
            }
        }
    }
    
    /**
     * Method called when deleting a record.
     * @access public
     */
    public function delete()
    {
        $params = $this->uri->uri_to_assoc();
        if( isset($params['id']) && valid_get_id($params['id']) )
        {
            // delete from database
            $deleted = $this->role_model->delete(array('id' => $params['id']));
            if($deleted === 'deleted')
            {
                view_roles_list(NULL, 'delete_success');
            } else
            {
                view_roles_list(NULL, 'archived');
            }
        } else
        {
            $this->show_404();
        }
    }
    
    /*
     * Preview information about a record
     * @access public
     */
    public function preview()
    {
        $params = $this->uri->uri_to_assoc();
        if( isset($params['id']) && valid_get_id($params['id']) )
        {
            $role = $this->role_model->get_by_id($params['id']);
            view_role_preview($role);
        } else
        {
            $this->show_404();
        }
    }
    
    /**
     * Method called when deleting multiple roles.
     * @access public
     */
    public function delete_multiple()
    {        
        if(valid_ids())
        {
            $items = $this->input->post('items');
            $archived = $this->role_model->until_it_sleeps($items);
            
            if(count($archived) > 0)
            {
                view_list(NULL, 'archived');
            } else
            {
                view_list(NULL, 'delete_success');
            }
        } else
        {
            $this->show_404();
        }
    }
    
}

/* End of file role.php */
/* Location: ./application/roles/cms-roles/controllers/role.php */